console.log('~!@#$%^&**&^%$#@!~ ' + new Date().toLocaleString());
var default_morning_hour = 8;
var default_morning_minute = 30;
var default_evening_hour = 18;
var default_evening_minute = 10;
var cancelDkkk = false;
var cancelDkkkCount = 0;
document.addEventListener('keydown', (event => {
	setTimeout(function () {
		cancelDkkkCount = 0;
	}, 2 * 1000);
	if (event.key.toLowerCase() == 'w') {
		cancelDkkkCount++;
	}
	if (cancelDkkkCount >= 5) {
		cancelDkkk = true;
	}
}))

var today = new Date();
var today_year = today.getFullYear();
var _today_month = today.getMonth() + 1;
today_month = _today_month + '';
today_month = today_month.length == 1 ? '0' + today_month : today_month;
var _today_date = today.getDate();
today_date = _today_date + '';
today_date = today_date.length == 1 ? '0' + today_date : today_date;
var today_hour = today.getHours();
var today_minute = today.getMinutes();
var today_ymd = today_year + '-' + today_month + '-' + today_date;

var znhcjdlyhm = '';
var znhcjdlmm = '';
var znhcjdlsfqy = true;
var todaysfqy = true;
var wsbzxb = false;
var znhcjdlwsbzxbbz = ' ';
chrome.storage.local.get(['znhcjdlyhm', 'znhcjdlmm', 'znhcjdlsfqy',
	'znhcjdlwsbdk', 'znhcjdlzxbdk', 'znhcjdlwsbzxbbz', 'znhcjdlhlrqarea'], function (data) {
		znhcjdlyhm = data.znhcjdlyhm;
		znhcjdlmm = data.znhcjdlmm;
		znhcjdlsfqy = !(data.znhcjdlsfqy === '0');
		var znhcjdlwsbdk = data.znhcjdlwsbdk;
		var znhcjdlzxbdk = data.znhcjdlzxbdk;
		znhcjdlwsbzxbbz = data.znhcjdlwsbzxbbz ? data.znhcjdlwsbzxbbz : ' ';
		var znhcjdlhlrqarea = data.znhcjdlhlrqarea;
		if (znhcjdlwsbdk) {
			try {
				var znhcjdlwsbdkDate = new Date(znhcjdlwsbdk);
				if (today_year == znhcjdlwsbdkDate.getFullYear()
					&& _today_month == (znhcjdlwsbdkDate.getMonth() + 1)
					&& _today_date == znhcjdlwsbdkDate.getDate()) {
					default_morning_hour = znhcjdlwsbdkDate.getHours();
					default_morning_minute = znhcjdlwsbdkDate.getMinutes();
					wsbzxb = true;
				}
			} catch (error) {
				console.error(error);
			}
		}
		if (znhcjdlzxbdk) {
			try {
				var znhcjdlzxbdkDate = new Date(znhcjdlzxbdk);
				if (today_year == znhcjdlzxbdkDate.getFullYear()
					&& _today_month == (znhcjdlzxbdkDate.getMonth() + 1)
					&& _today_date == znhcjdlzxbdkDate.getDate()) {
					default_evening_hour = znhcjdlzxbdkDate.getHours();
					default_evening_minute = znhcjdlzxbdkDate.getMinutes();
					wsbzxb = true;
				}
			} catch (error) {
				console.error(error);
			}
		}
		if (znhcjdlhlrqarea) {
			var znhcjdlhlrqareaDate = znhcjdlhlrqarea.split('/');
			todaysfqy = znhcjdlhlrqareaDate.indexOf(today_ymd) == -1;
		}
		dododo();
	})

function dododo() {
	if (!znhcjdlsfqy) {
		console.warn("dkkk未启用");
		return;
	}
	function toDkkk(fun, timeout) {
		if (timeout && timeout > 0) {
			fun && typeof fun == 'function' && setTimeout(function () {
				if (cancelDkkk) {
					console.log('canceled the kk');
					return;
				}
				fun();
			}, timeout);
		} else {
			if (cancelDkkk) {
				console.log('canceled the kk');
				return;
			}
			fun && typeof fun == 'function' && fun();
		}
	}
	var dkkkUrl1 = 'http://dk.com';
	var dkkkUrl2 = 'http://dk.com/';
	var dkkkUrl3 = 'http://dk.com/home';
	var dkkkUrl = 'http://dk.com/dk';
	var dkkkLoginUrl = 'http://dk.com/login';
	var currentUrl = window.location.href;
	if (currentUrl == dkkkUrl1 || currentUrl == dkkkUrl2 || currentUrl == dkkkUrl3) {
		toDkkk(() => {
			window.location.href = dkkkUrl;
		}, 10 * 1000);
	}
	if (currentUrl == dkkkLoginUrl) {
		toDkkk(() => {
			document.getElementById('account').value = znhcjdlyhm;
			document.getElementById('account').dispatchEvent(new Event('input', { bubbles: true }))
			document.getElementById('password').value = znhcjdlmm;
			document.getElementById('password').dispatchEvent(new Event('input', { bubbles: true }))
			document.querySelector('button[type=submit]').click();
			toDkkk(() => {
				document.getElementById('account').value = znhcjdlyhm;
				document.getElementById('account').dispatchEvent(new Event('input', { bubbles: true }))
				document.getElementById('password').value = znhcjdlmm;
				document.getElementById('password').dispatchEvent(new Event('input', { bubbles: true }))
				document.querySelector('button[type=submit]').click();
			}, 3 * 1000);
		}, 10 * 1000);
	} else if (currentUrl.startsWith(dkkkUrl1)) {
		toDkkk(() => {
			window.location.href = dkkkUrl3;
		}, 10 * 1000);
	}
	function getTip() {
		var tipDate = new Date();
		var gd = window.localStorage.getItem('dkkkbbbbbbbbbbbb') === '1';
		var container = document.createElement('div');
		var tip = document.createElement('div');
		if (gd) {
			container.style = 'position: absolute;top: 0px;left: 37%;transition: transform 0.5s ease;';
			tip.style = 'height: 71px;overflow: hidden;color: red;font-size: 20px;cursor: default;border-bottom: 5px solid red;';

		} else {
			container.style = 'position: absolute;top: -65px;left: 37%;transition: transform 0.5s ease;';
			tip.style = 'height: 71px;overflow: hidden;color: red;font-size: 20px;cursor: default;border-bottom: 5px solid red;';

		}
		var fixBtn;
		container.onmouseenter = function (e) {
			!gd && (e.target.style.transform = 'translateY(65px)');
			fixBtn = document.createElement('button');
			fixBtn.style = 'background-image: radial-gradient(rgb(255 0 0), rgb(255 255 255));border: none;border-radius: 7px;cursor: pointer;';
			fixBtn.innerText = gd ? '取消固定' : '固定';
			fixBtn.onclick = function () {
				window.localStorage.setItem('dkkkbbbbbbbbbbbb', gd ? '0' : '1');
				container.remove();
				document.body.append(getTip());
			}
			container.appendChild(fixBtn);
		}
		container.onmouseleave = function (e) {
			!gd && (e.target.style.transform = 'translateY(0px)');
			if (fixBtn) {
				fixBtn.remove();
			}
		}
		container.appendChild(tip);
		var pStyle = ' style = "margin: 0;" ';
		var updateCalendarTip = '';
		if (tipDate.getMonth() + 1 > 10 && tipDate.getFullYear() + 1 != window.wurdCurrentYear) {
			updateCalendarTip = '<span style="font-size: 12px;">⏰年底了，注意更新下年节假日</span>';
		}
		var sbTip = '';
		if (window.isHhhhh()) {
			sbTip = '今天不上班';
		} else {
			sbTip = '今天上班要打卡';
		}
		var p1 = '<p ' + pStyle + '>' + sbTip + ' ' + tipDate.toLocaleString() + ' ' + updateCalendarTip + '</p>';
		var tipDateTime = tipDate.getTime();
		var nextBBDay = [];
		while (nextBBDay.length < 5) {
			tipDateTime = tipDateTime + (24 * 60 * 60 * 1000)
			var d = new Date(tipDateTime);
			if (!window.isHhhhh(d)) {
				nextBBDay.push(d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate());
			}
		}
		var p2 = '<p ' + pStyle + '>' + nextBBDay.join('⚔') + '</p>';
		tip.innerHTML = p1 + p2;
		return container;
	}
	function doClockInOrOut() {
		setInterval(function () { window.location.reload(); }, 10 * 60 * 1000 + Math.floor(Math.random() * 111111));
		document.body.append(getTip());
		if (window.isHhhhh()) {

		} else {
			toDkkk(() => {
				if (todaysfqy) {
					if (wsbzxb) {
						var aarea = document.querySelector("textarea");
						aarea.value = znhcjdlwsbzxbbz;
						var e1 = new Event('input', { bubbles: true });
						aarea.dispatchEvent(e1);
						var e2 = new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter' });
						aarea.dispatchEvent(e2)
					}
					if (document.querySelector("[class='ant-radio-input']:checked").value == '1') {
						var cD = document.querySelector('div[class="ant-table-wrapper searchtabledataNull"]').querySelector('span[class="ant-typography ant-typography-ellipsis ant-typography-single-line ant-typography-ellipsis-single-line"]').innerHTML;

						if (!cD.startsWith(today_ymd) && today_hour >= default_morning_hour && today_minute > default_morning_minute) {
							document.querySelector('button[class="ant-btn ant-btn-primary"]').click();
						}
					}
					if (document.querySelector("[class='ant-radio-input']:checked").value == '2') {
						var cD = document.querySelector('div[class="ant-table-wrapper searchtabledataNull"]').querySelector('span[class="ant-typography ant-typography-ellipsis ant-typography-single-line ant-typography-ellipsis-single-line"]').innerHTML;

						if ((!cD.startsWith(today_ymd) || document.querySelector('span[class="ant-typography"]').innerHTML != '签退')
							&& today_hour >= default_evening_hour && today_minute > default_evening_minute) {
							document.querySelector('button[class="ant-btn ant-btn-primary"]').click();
						}
					}
				}
			}, 10 * 1000);
		}
	}
	if (currentUrl == dkkkUrl) {
		console.log('dddddddddoooododododoooo');
		doClockInOrOut();

	}


	var dbClickFun = function (e) {
		console.log(e.target);
		e.target.remove();
	}
	// left
	imgEle = document.createElement('img');
	imgEle.style = 'transform: rotateY(360deg);';
	imgEle.src = chrome.runtime.getURL("icon.png");
	imgEle.ondblclick = dbClickFun;
	//document.getElementsByTagName('body')[0].appendChild(imgEle)

	imgEle = document.createElement('img');
	imgEle.style = 'transform: rotateY(180deg);';
	imgEle.src = chrome.runtime.getURL("icon.png");
	imgEle.ondblclick = dbClickFun;
	//document.getElementsByTagName('body')[0].appendChild(imgEle)
	// right
	imgEle = document.createElement('img');
	imgEle.style = 'transform: rotateY(180deg);float: right;';
	imgEle.src = chrome.runtime.getURL("icon.png");
	imgEle.ondblclick = dbClickFun;
	//document.getElementsByTagName('body')[0].appendChild(imgEle)

	imgEle = document.createElement('img');
	imgEle.style = 'float: right;transform: rotateY(360deg);';
	imgEle.src = chrome.runtime.getURL("icon.png");
	imgEle.ondblclick = dbClickFun;
	//document.getElementsByTagName('body')[0].appendChild(imgEle)

}
